<?php
// +----------------------------------------------------------------------
// | HkCms
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace common\model;

use think\Validate;

class Cache extends Model
{
    protected $_validate = array(
        'name'=>'require',
        'model'=>'require',
        'action'=>'require',
    );

    protected $_validate_msg = [
        'name.require' => '缓存名称不能为空！',
        'model.require' => '模型名称不能为空！',
        'action.require' => '模型方法名称不能为空！',
    ];

//    protected $auto = ['system'=>0];

    protected static $__instance;
    public static function getInterface()
    {
        if (static::$__instance instanceof static) {
            return static::$__instance;
        }
        return static::$__instance = new static();
    }

    /**
     * 执行更新缓存
     * @param array $config 缓存配置
     * @return boolean
     */
    public function runUpdate(array $config)
    {
        if (empty($config)) {
            return false;
        }

        $config['module'] = strtolower($config['module']);
        $model = '';
        if (empty($config['module'])) { // 空模块默认为common
            $class = 'common\model\\'.$config['model'];
            $model = new $class;
        } else {
            $model = "{$config['module']}/{$config['model']}";
            $model = model($model);
        }

        if ($config['action']) {
            $action = $config['action'];
            $model->$action();
            return true;
        }
        return false;
    }

    /**
     * 获取需要更新缓存列队
     * @param boolean $isup 是否强制获取最新
     * @return array
     */
    public function getCacheList($isup = false)
    {
        $cache = config('cache_list');
        if (empty($cache) || $isup) {
            //取得现在全部需要更新缓存的队列
            $cache = $this->order(array('id' => 'ASC'))->select()->toArray();
            config('cache_list', $cache, 600);
        }
        return $cache;
    }

    /**
     * 安装模块，注册缓存
     * @param array $cache 缓存配置
     * @param array $config 模块配置
     * @return boolean
     */
    public function installModuleCache(array $cache, array $config)
    {
        if (empty($cache) || empty($config)) {
            $this->error = '参数不完整！';
            return false;
        }
        $module = $config['module'];
        $data = array();
        foreach ($cache as $key => $rs) {
            $add = array(
                'key' => $key,
                'name' => $rs['name'],
                'module' => isset($rs['module'])? $rs['module'] : $module,
                'model' => $rs['model'],
                'action' => $rs['action'],
                'param' => isset($rs['param'])? $rs['param']: '',
                'system' => 0,
            );

            $validate = Validate::make($this->_validate,$this->_validate_msg);
            if (!$validate->check($add)) {
                $this->error = $validate->getError();
                return false;
            }
            $data[] = $add;
        }
        if (!empty($data)) {
            return $this->saveAll($data) !== false ? true : false;
        }
        return true;
    }

    /**
     * 删除指定模块下的全部缓存队列
     * @param string $module 模块名称
     * @return boolean
     */
    public function deleteCacheModule($module)
    {
        if (empty($module)) {
            $this->error = '请指定模块！';
            return false;
        }
        if ($this->where(array('module' => $module, 'system' => 0))->delete() !== false) {
            return true;
        } else {
            $this->error = '删除失败！';
            return false;
        }
    }
}